package com.wx.mall.operate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wx.mall.operate.dto.SeckillTimeDto;
import com.wx.mall.operate.entity.SeckillTime;
import com.wx.mall.operate.mapper.SeckillTimeMapper;
import com.wx.mall.operate.service.ISeckillTimeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author j
 * @since 2020-05-28
 */
@Service
public class SeckillTimeServiceImpl extends ServiceImpl<SeckillTimeMapper, SeckillTime> implements ISeckillTimeService {

    @Override
    public List<SeckillTime> findTime() {
        QueryWrapper<SeckillTime> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("seckill_delete",1).orderByAsc("start_time");
        List<SeckillTime> list=this.list(queryWrapper);
        return list;
    }

    @Override
    public List<SeckillTime> findTimeTwo() {
        QueryWrapper<SeckillTime> wrapper=new QueryWrapper<>();
        wrapper.eq("seckill_type",1).eq("seckill_delete",1).orderByAsc("start_time");
        List<SeckillTime> list=this.list(wrapper);
        return list;
    }

    @Override
    public int updateTimeList(SeckillTimeDto dto) {
        SeckillTime seckillTime=new SeckillTime();
        seckillTime.setId(dto.getId());
        seckillTime.setSeckillName(dto.getSeckillName());
        seckillTime.setStartTime(dto.getStartTime());
        seckillTime.setEndTime(dto.getEndTime());
        seckillTime.setSeckillType(dto.getSeckillType());
        boolean success=this.updateById(seckillTime);
        return success?1:0;
    }

    @Override
    public int addTimeList(SeckillTimeDto dto) {
        Random random=new Random();
        int a=random.nextInt(100)+1;
        SeckillTime seckillTime=new SeckillTime();
        seckillTime.setSeckillName(dto.getSeckillName());
        seckillTime.setStartTime(dto.getStartTime());
        seckillTime.setEndTime(dto.getEndTime());
        seckillTime.setSeckillType(dto.getSeckillType());
        seckillTime.setSkuNum(a);
        seckillTime.setSeckillDelete(1);
        boolean save = this.save(seckillTime);
        return save?1:0;
    }

    @Override
    public int deletes(Integer id) {
        List<SeckillTime> list=new ArrayList<>();
        SeckillTime seckillTime=this.getById(id);
        seckillTime.setSeckillDelete(0);
        System.out.println(seckillTime);
        list.add(seckillTime);
        boolean b = this.updateBatchById(list);
        return b?1:0;
    }
}
